package com.fnzn.entranceGuard.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fnzn.entranceGuard.entity.GuardDevice;
import com.fnzn.entranceGuard.entity.GuardRoleDevice;
import com.fnzn.entranceGuard.mapper.GuardRoleDeviceMapper;
import com.fnzn.entranceGuard.service.IGuardRoleDeviceService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sxf
 * @since 2018-06-26
 */
@Slf4j
@Service
public class GuardRoleDeviceServiceImpl extends ServiceImpl<GuardRoleDeviceMapper, GuardRoleDevice> implements IGuardRoleDeviceService {

    @Autowired
    private GuardRoleDeviceMapper guardRoleDeviceMapper;

    @Override
    public boolean updateByRoleId(Integer roleId, String[] deviceIds) {
        try{
            //先删除
            EntityWrapper<GuardRoleDevice> wrapper = new EntityWrapper();
            wrapper.eq("role_id",roleId);
            this.delete(wrapper);
            //新增
            GuardRoleDevice guardRoleDevice = new GuardRoleDevice();
            guardRoleDevice.setRoleId(roleId);
            StringBuffer sb = new StringBuffer();
            for (String str :deviceIds) {
                sb.append(str);
                sb.append(",");
            }
            guardRoleDevice.setDeviceIds(sb.substring(0,sb.length()-1));
            this.insert(guardRoleDevice);
            return true;
        }catch (Exception e){
            log.error(" GuardRoleDevice 添加错误：",e);
            e.printStackTrace();
        }
        return false;

    }


}
